//
//  MainViewController.swift
//  云Sir的微博
//
//  Created by 赵云 on 15/10/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
      
    }
    
    override func viewDidAppear(animated: Bool) {
        setupComposeButton()
    }
    
    //设置button的位置
    private func setupComposeButton(){
        
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        
        composeButton.frame = CGRectOffset(rect, w * 2, 0)
        
    }
    
    //监听按钮的点击
    func clickButton(){
        
        print(__FUNCTION__)
        
    }
    
    //添加所有控制器
    //private  代表私有的
    //中间加号添加的思路:先创建一个UIViewController,完后在创建一个button来覆盖这块儿
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_home")
        addChildViewController(UIViewController())
        addChildViewController(DiscoveryTableViewController(), title: "发现", imageName: "tabbar_home")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_home")
        
    }

    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    
    //懒加载控件
    lazy private var composeButton: UIButton = {
        //创建button
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        
        button.addTarget(self, action: "clickButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
 

}
